package com.tchintech.pay.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.BankListAdapter;
import com.tchintech.pay.delegate.BankDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.Bank;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 银行选择列表
 */

public class BankActivity extends ActivityPresenter<BankDelegate> implements View.OnClickListener {
    private ListView listView;
    private String  Bound_Url="Bound.bound";
    private Map<String,String > map=new HashMap<String,String>();
    private Handler handler;
    private Bank bank;
    private List<Bank> list=new ArrayList<Bank>();
    private BankListAdapter bankListAdapter;



    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTitle("银行列表");
        viewDelegate.setVisible();
        listView=viewDelegate.getlistVIew();
        bankListAdapter=new BankListAdapter(this,list);
        listView.setAdapter(bankListAdapter);
        viewDelegate.setOnClickListener(this,R.id.iv_back);
        handler();
        queryBank();
        select();
    }
    private void queryBank(){
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, Bound_Url, map, HttpCode.QUERYBACK);
    }
    private void  handler(){
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what){
                    case  HttpCode.QUERYBACK:
                        try{
                            JSONObject json=new JSONObject(msg.obj.toString());
                            if(json.getString("ret").equals("1")){
                                setData(json.getString("data"));
                            }else{

                            }
                        }catch (Exception E){

                        }
                        break;
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                }
            }
        };

    }
    //设置值
    private void setData(String  obj){
        try{
            System.out.println("返回值object"+obj.toString());
            JSONArray jsonArray=new JSONArray(obj);
            System.out.println("返回值jsonArray"+obj);
           for(int i=0;i<jsonArray.length();i++){
                JSONObject josn=jsonArray.getJSONObject(i);
               System.out.println("返回值josn"+josn);
                bank=new Bank();
                bank.setBankName(josn.getString("bankName"));
                bank.setBankType(josn.getString("bankType"));
                list.add(bank);
            }
            bankListAdapter.setList(list);
        }catch (Exception e){
            System.out.println("返回值错误"+e);

        }
    }

    private void select(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取点击行的值
                Bank bank= (Bank) parent.getItemAtPosition(position);
                Intent intent=new Intent();
                intent.setClass(BankActivity.this,BoundcaedActivity.class);
                intent.putExtra("bankName", bank.getBankName());
                intent.putExtra("bankType", bank.getBankType());
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected Class<BankDelegate> getDelegateClass() {
        return BankDelegate.class;
    }
}
